package com.dts.yjnb.service.controller.admin;

import com.dts.common.core.controller.BaseController;
import com.dts.common.core.domain.AjaxResult;
import com.dts.common.core.page.TableDataInfo;
import com.dts.yjnb.service.domainDto.CreditInfoDTO;
import com.dts.yjnb.service.service.ICreditInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;

import java.util.List;

/**
 * 信贷信息Controller
 *
 * @author dts
 */
@Api(tags = "信贷信息管理")
@RestController
@RequestMapping("/yjnb/creditInfo")
public class CreditInfoController extends BaseController
{

    @Autowired
    private ICreditInfoService creditInfoService;

    /**
     * 信贷信息分页查询
     */
    @GetMapping("/page")
    @ApiOperation("信贷信息分页查询")
    public TableDataInfo page(CreditInfoDTO creditInfoDTO) {
        return creditInfoService.pageQuery(creditInfoDTO);
    }

    /**
     * 根据ID查询信贷信息详情
     */
    @GetMapping("/getByid/{id}")
    @ApiOperation("根据ID查询信贷信息详情")
    public AjaxResult getByid(@PathVariable Long id) {
        return AjaxResult.success(creditInfoService.getByid(id));
    }

    /**
     * 搜索信贷信息
     */
    @GetMapping("/search")
    @ApiOperation("搜索信贷信息")
    public TableDataInfo search(CreditInfoDTO creditInfoDTO) {
        startPage();
        return getDataTable(creditInfoService.search(creditInfoDTO));
    }

    /**
     * 新增信贷信息
     */
    @PostMapping("/add")
    @ApiOperation("新增信贷信息")
    public AjaxResult add(@Valid @RequestBody CreditInfoDTO creditInfoDTO) {
        if (creditInfoService.add(creditInfoDTO)) {
            return AjaxResult.success("新增成功");
        }
        return AjaxResult.error("新增失败");
    }

    /**
     * 修改信贷信息
     */
    @PutMapping("/update")
    @ApiOperation("修改信贷信息")
    public AjaxResult update(@Valid @RequestBody CreditInfoDTO creditInfoDTO) {
        if (creditInfoService.update(creditInfoDTO)) {
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.error("修改失败");
    }

    /**
     * 批量删除信贷信息
     */
    @DeleteMapping("/deletes")
    @ApiOperation("信贷信息批量删除")
    public AjaxResult deleteByids(@RequestParam List<Long> ids) {
        creditInfoService.deleteBatch(ids.toArray(new Long[0]));
        return AjaxResult.success("删除成功");
    }

    /**
     * 批量修改信贷信息状态
     */
    @PutMapping("/updateStatus")
    @ApiOperation("批量修改信贷信息状态")
    public AjaxResult updateStatus(@RequestParam List<Long> ids, @RequestParam Integer status) {
        creditInfoService.updateStatus(ids.toArray(new Long[0]), status);
        String message = status == 1 ? "启用成功" : "禁用成功";
        return AjaxResult.success(message);
    }

    /**
     * 批量修改信贷信息推荐状态
     */
    @PutMapping("/updateRecommend")
    @ApiOperation("批量修改信贷信息推荐状态")
    public AjaxResult updateRecommend(@RequestParam List<Long> ids, @RequestParam Integer isRecommend) {
        creditInfoService.updateRecommend(ids.toArray(new Long[0]), isRecommend);
        String message = isRecommend == 1 ? "推荐成功" : "取消推荐成功";
        return AjaxResult.success(message);
    }
}
